<template>
    <div class="page-immediately-pay">
    </div>
</template>

<script>
    import {initPayEnvironment, pay_order} from "../dev/core/utils/pay";
    import {TbOrderService} from "../dev/services/tb-order.service";
    import {CommonUtil} from "../dev/core/utils/common.util";

    export default {
        props: [],
        data: function () {
            return {
                list: [],
                isShowActionSheet: false,
                params: {
                    tbOrder: null,
                    callback: function (tbOrder) {  // 需要重写
                    }
                }
            }
        },
        methods: {
            pay(params) {
                const _self = this;
                this.params = params;

                // 仅允许在线支付
                if (!this.globalVariable.userInfo.tbCommunityUser || this.globalVariable.userInfo.tbCommunityUser.payType === null
                    || this.globalVariable.userInfo.tbCommunityUser.payType === "0") {
                    console.log("仅允许在线支付");
                    initPayEnvironment().then(result => {
                        _self.wxpay();
                    });
                } else if (this.globalVariable.userInfo.tbCommunityUser.payType === "1") {  // 仅允许线下支付
                    console.log("仅允许线下支付");
                    this.offlinePay();
                } else if (this.globalVariable.userInfo.tbCommunityUser.payType === "2") {  // 同时支持在线和线下支付
                    console.log("同时支持在线和线下支付");
                    this.isShowActionSheet = true;
                    uni.showActionSheet({
                        itemList: ['微信支付', '线下支付'],
                        success: function (res) {
                            if (res.tapIndex === 0) {
                                initPayEnvironment().then(result => {
                                    _self.wxpay();
                                });
                            } else if (res.tapIndex === 1) {
                                _self.offlinePay();
                            }
                        },
                        fail: function (res) {
                            console.log(res.errMsg);
                        }
                    });
                } else {
                    console.log("该用户无法完成支付");
                    CommonUtil.toast('该用户无法完成支付');
                }
            },
            /**
             * 微信支付
             */
            wxpay() {
                const _self = this;
                this.isShowActionSheet = false;
                // 订单已存在
                if (_self.params.tbOrder.id) {
                    // 支付订单
                    pay_order({
                        orderId: _self.params.tbOrder.id,
                        callback: function (payResult) {
                            _self.params.callback(_self.params.tbOrder, payResult);
                        }
                    })
                } else {  // 保存新订单
                    new TbOrderService().saveTbOrder(_self.params.tbOrder).then(saveResult => {
                        if (saveResult.status === 'success') {
                            _self.params.tbOrder = saveResult.tbOrder;

                            // 支付订单
                            pay_order({
                                orderId: saveResult.tbOrder.id,
                                callback: function (payResult) {
                                    _self.params.callback(_self.params.tbOrder, payResult);
                                }
                            })
                        } else {
                            CommonUtil.alert(saveResult.message)
                        }
                    })
                }
            },
            /**
             * 线下支付
             */
            offlinePay() {
                const _self = this;
                this.isShowActionSheet = false;
                CommonUtil.alert({
                    title: '提示！',
                    content: '确定线下支付吗？',
                    showCancel: true, // 是否显示取消按钮
                    success: function (res) {
                        if (res.cancel) { // 点击取消,默认隐藏弹框
                        } else { // 点击确定
                            // 订单已存在
                            if (_self.params.tbOrder.id) {
                                // 离线支付
                                new TbOrderService().offlinePay({id: _self.params.tbOrder.id}).then(result => {
                                    if (result.status === 'success') {
                                        CommonUtil.toast("下单成功").then(() => {
                                            _self.params.callback(_self.params.tbOrder, _self.params.tbOrder);
                                        })
                                    } else {
                                        CommonUtil.alert(result.message);
                                    }
                                });
                            } else {  // 保存新订单
                                new TbOrderService().saveTbOrder(_self.params.tbOrder).then(saveResult => {
                                    if (saveResult.status === 'success') {
                                        _self.params.tbOrder = saveResult.tbOrder;

                                        // 离线支付
                                        new TbOrderService().offlinePay({id: saveResult.tbOrder.id}).then(payResult => {
                                            if (payResult.status === 'success') {
                                                CommonUtil.toast("下单成功").then(() => {
                                                    _self.params.callback(_self.params.tbOrder, _self.params.tbOrder);
                                                })
                                            } else {
                                                CommonUtil.alert(payResult.message);
                                            }
                                        });
                                    } else {
                                        CommonUtil.alert(saveResult.message)
                                    }
                                })
                            }
                        }
                    }
                });
            },
            /**
             * 取消支付
             * @param event
             */
            onCancelActionSheet(event) {
                this.isShowActionSheet = false;
            },
        },
    }
</script>

<style lang="scss">
    .page-immediately-pay {
    }
</style>
